<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\BaseController;
use App\Http\Requests\Auth\CodeRequest;
use App\Mail\SendCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Overtrue\EasySms\EasySms;

class BindContrpller extends BaseController
{
    public function emailCode(Request $request)
    {
        $request->validate([
            'email' => 'required|email'
        ]);

        //发送邮箱
        $email=$request->email;
        Mail::to($email)->send(new SendCode($email));
        return $this->response->noContent();
    }

    //更新邮箱
    public function updateEmail(CodeRequest $request){
        //验证code
        if (cache('email_code_'.$request->email)!=$request->code){
            return $this->response->errorBadRequest('邮箱或者验证码错误');
        }

        $user=auth('api')->user();
        $user->email = $request->email;
        $user->save();
        return $this->response->noContent();
    }

}
